<script setup>
import { User, Lock } from '@element-plus/icons-vue'
import { ref } from 'vue'
import { userRegisterServer } from '@/api/user'
const isRegister = ref(false)
const login = () => {
  console.log('登录')
}
const register = async () => {
  /*
  注册事件：
  1.通过const from = ref() 和 <el-form ref="from">获取到el-form的DOM元素
  2.element里面(from.value)表单，有validator(),检验的函数，来检验用户输入是否正确
  3.表单，有validator()函数里面有抛出异常
  */
  await from.value.validator()
  await userRegisterServer(fromModel.value)
  alert('注册成功')
  isRegister.value = false
}
// 基于 element plus 的表单，需要绑定一个表单模型
const fromModel = ref({
  username: '',
  password: '',
  repassword: ''
})
// 整个表单的校验规则
// 1. 非空校验 required: true      message消息提示，  trigger触发校验的时机 blur change
// 2. 长度校验 min:xx, max: xx
// 3. 正则校验 pattern: 正则规则    \S 非空字符
// 4. 自定义校验 => 自己写逻辑校验 (校验函数)
//    validator: (rule, value, callback)
//    (1) rule  当前校验规则相关的信息
//    (2) value 所校验的表单元素目前的表单值
//    (3) callback 无论成功还是失败，都需要 callback 回调
//        - callback() 校验成功
//        - callback(new Error(错误信息)) 校验失败
const rules = {
  // required:是否为空  message:提示信息 trigger:什么时候触发
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 5, max: 10, message: '用户名必须是 5-10位 的字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码必须是 6-15位 的非空字符',
      trigger: 'blur'
    }
  ],
  repassword: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码必须是 6-15位 的非空字符',
      trigger: 'blur'
    },
    {
      validator: (rule, value, callback) => {
        // 判断 value 和 当前 form 中收集的 password 是否一致
        if (value !== fromModel.value.password) {
          callback(new Error('两次输入密码不一致'))
        } else {
          callback() // 就算校验成功，也需要callback
        }
      },
      trigger: 'blur'
    }
  ]
}
// 这个from是为了跟下面的element的from做绑定。获取DOM里面的element from实例
const from = ref()
</script>

<template>
  <!-- 
       (1) el-form => :model="ruleForm"      绑定的整个form的数据对象 { xxx, xxx, xxx }
       (2) el-form => :rules="rules"         绑定的整个rules规则对象  { xxx, xxx, xxx }
       (3) 表单元素 => v-model="ruleForm.xxx" 给表单元素，绑定form的子属性
       (4) el-form-item => prop配置生效的是哪个校验规则 (和rules中的字段要对应)
       -->
  <el-row class="login-page">
    <!-- 左边背景图 -->
    <el-col :span="12" class="bg"></el-col>
    <!-- 输入from -->
    <el-col :span="6" :offset="3" size="large" class="form" :model="from">
      <!-- 登录相关 -->
      <el-form v-if="!isRegister" :model="fromModel" :rules="rules" ref="from">
        <el-form-item>
          <h1>登录</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input
            type="text"
            :prefix-icon="User"
            placeholder="请输入用户名"
            v-model="fromModel.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            :prefix-icon="Lock"
            placeholder="请输入密码"
            v-model="fromModel.password"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <div class="flex">
            <el-checkbox>记住我</el-checkbox>
            <el-link type="primary" :underline="false">忘记密码？</el-link>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button
            @click="login"
            size="large"
            class="button"
            type="primary"
            auto-insert-space
            >登录</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-link type="info" :underline="false" @click="isRegister = true">
            注册 →
          </el-link>
        </el-form-item>
      </el-form>
      <!-- 注册相关 -->
      <el-form v-else :model="fromModel" :rules="rules" ref="from">
        <el-form-item>
          <h1>注册</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input
            type="text"
            :prefix-icon="User"
            placeholder="请输入用户名"
            v-model="fromModel.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            :prefix-icon="Lock"
            placeholder="请输入密码"
            v-model="fromModel.password"
          ></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            type="password"
            :prefix-icon="Lock"
            placeholder="请再次输入密码"
            v-model="fromModel.repassword"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            @click="register"
            size="large"
            class="button"
            type="primary"
            auto-insert-space
            >注册</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-link type="info" :underline="false" @click="isRegister = false">
            ← 返回
          </el-link>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<style lang="scss" scoped>
.login-page {
  // 屏幕分成 100等分，1vw或者1vh = 屏幕的1/100
  height: 100vh;
  background: #fff;
  .bg {
    background:
      url('@/assets/logo2.png') no-repeat 60% center / 240px auto,
      url('@/assets/login_bg.jpg') no-repeat center / cover;
    border-radius: 0 20px 20px 0;
  }
  .form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    user-select: none; // 禁止复制
    .flex {
      width: 100%; //需要指定宽度，不然就让子控件内容撑开了
      display: flex;
      justify-content: space-between;
    }
    .button {
      width: 100%;
    }
  }
}
</style>
